var d = new Date();
$("#curdate").text(d.toLocaleString()); 

$(document).on("click", "#randombtn", function () {
    var namestr = $("#name").val();
    var namearray = new Array();
    
    var array = namestr.match(/(\d+)\.\s*([\u4E00-\u9FA5A-Za-z])*/g);
    console.log("接龙解析：", array)

    //namearray = namestr.split(/[\n\s+,，；;、]/g);
    namearray = array;

    for (var i = 0; i < namearray.length; i++) {
        if (namearray[i] == "") {
            // 删除数组中空值
            namearray.splice(i, 1);
            i--;
        }
        if (namearray[i].length < 4) {
            // 删除数组中错误的值
            namearray.splice(i, 1);
            i--;
        }
    }
    console.log(namestr);
    console.log("原始数组: ", namearray);

    shuffle(namearray)

    console.log("随机分组后", namearray)

    a = parseInt(namearray.length / 2);
    console.log("players number: ", namearray.length);
    $("#groupa").text("A组球员(" + a + "人) ：");
    $("#groupb").text("B组球员(" + (namearray.length - a) + "人) ：");

    for (i = 0; i < a; i++) {
        if (i < a - 1) {
            $("#groupa").append(namearray[i] + "，");
        } else {
            $("#groupa").append(namearray[i] + "。");
        }
    }
    for (i = a; i < namearray.length; i++) {
        if (i < namearray.length - 1) {
            $("#groupb").append(namearray[i] + "，");
        } else {
            $("#groupb").append(namearray[i] + "。");
        }
    }

    $.toast("分组成功", function () {
        console.log('close');
    });

});

function shuffle(arr) {
    let i = arr.length;
    while (i) {
        let j = Math.floor(Math.random() * i--);
        [arr[j], arr[i]] = [arr[i], arr[j]];
    }
}


function settime() {
    var d = new Date();
    $("#curdate").text(d.toLocaleString());
}
var ref = setInterval(settime, 1000);

function saveText(text, filename) {
    var a = document.createElement('a');
    a.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
    a.setAttribute('download', filename);
    a.click()
}
